﻿using System.Linq;
using Abp.Configuration;
using Abp.Localization;
using Abp.Net.Mail;
using KDS.Mes.EntityFramework;
using KDS.Mes.Mes.Factorys;
using KDS.Mes.Mes.Workshops;
using KDS.Mes.Mes.Lines;
using KDS.Mes.Mes.Storages;
using KDS.Mes.Mes.Colors;
using KDS.Mes.Mes.Materials;
using KDS.Mes.Mes.Cartypes;
using KDS.Mes.Mes.SeatPositions;
using KDS.Mes.Mes.PlanModes;
using KDS.Mes.Mes.ProductModes;
using System.Collections.Generic;

namespace KDS.Mes.Migrations.SeedData
{
    public class InitialDatasCreator
    {
        private readonly MesDbContext _context;
        public static List<Factory> Factorys { get; private set; }
        public static List<Workshop> Workshops { get; private set; }
        public static List<Line> Lines { get; private set; }
        public static List<Storage> Storages { get; private set; }
        public static List<Color> Colors { get; private set; }
        public static List<Material> Materials { get; private set; }
        public static List<Cartype> Cartypes { get; private set; }
        public static List<SeatPosition> SeatPositions { get; private set; }
        public static List<PlanMode> PlanModes { get; private set; }
        public static List<ProductMode> ProductModes { get; private set; }

        public InitialDatasCreator(MesDbContext context)
        {
            _context = context;
        }

        public void Create()
        {

            CreateDatas();

        }

        private void CreateDatas()
        {
             foreach (var productMode in ProductModes)
            {
                AddProductModeIfNotExists(productMode);
            }

            foreach (var factory in Factorys)
            {
                AddFactoryIfNotExists(factory);
            }

            foreach (var workshop in Workshops)
            {
                AddWorkshopIfNotExists(workshop);
            }

            foreach (var line in Lines)
            {
                AddLineIfNotExists(line);
            }

            foreach (var storage in Storages)
            {
                AddStorageIfNotExists(storage);
            }

            foreach (var color in Colors)
            {
                AddColorIfNotExists(color);
            }

            foreach (var material in Materials)
            {
                AddMaterialIfNotExists(material);
            }

            foreach (var cartype in Cartypes)
            {
                AddCartypeIfNotExists(cartype);
            }

            foreach (var seatPosition in SeatPositions)
            {
                AddSeatPositionIfNotExists(seatPosition);
            }

            foreach (var planMode in PlanModes)
            {
                AddPlanModeIfNotExists(planMode);
            }

           
        }

        private void AddFactoryIfNotExists(Factory factory)
        {
            if (_context.Factorys.Any(l => l.FactoryName == factory.FactoryName && l.FactoryCode == factory.FactoryCode))
            {
                return;
            }
            _context.Factorys.Add(factory);
            _context.SaveChanges();
        }

        private void AddWorkshopIfNotExists(Workshop workshop)
        {
            if (_context.Workshops.Any(l => l.WorkshopName == workshop.WorkshopName && l.WorkshopCode == workshop.WorkshopCode && l.FactoryId == workshop.FactoryId))
            {
                return;
            }
            _context.Workshops.Add(workshop);
            _context.SaveChanges();
        }

        private void AddLineIfNotExists(Line line)
        {
            if (_context.Lines.Any(l => l.LineName == line.LineName && l.LineCode == line.LineCode && l.WorkshopId == line.WorkshopId))
            {
                return;
            }
            _context.Lines.Add(line);
            _context.SaveChanges();
        }

        private void AddStorageIfNotExists(Storage storage)
        {
            if (_context.Storages.Any(l => l.StorageName == storage.StorageName && l.StorageCode == storage.StorageCode))
            {
                return;
            }
            _context.Storages.Add(storage);
            _context.SaveChanges();
        }

        private void AddColorIfNotExists(Color color)
        {
            if (_context.Colors.Any(l => l.ColorName == color.ColorName && l.ColorCode == color.ColorCode))
            {
                return;
            }
            _context.Colors.Add(color);
            _context.SaveChanges();
        }

        private void AddMaterialIfNotExists(Material material)
        {
            if (_context.Materials.Any(l => l.MaterialName == material.MaterialName && l.MaterialCode == material.MaterialCode))
            {
                return;
            }
            _context.Materials.Add(material);
            _context.SaveChanges();
        }

        private void AddCartypeIfNotExists(Cartype cartype)
        {
            if (_context.Cartypes.Any(l => l.CartypeName == cartype.CartypeName && l.CartypeCode == cartype.CartypeCode))
            {
                return;
            }
            _context.Cartypes.Add(cartype);
            _context.SaveChanges();
        }

        private void AddSeatPositionIfNotExists(SeatPosition seatPosition)
        {
            if (_context.SeatPositions.Any(l => l.SeatPositionName == seatPosition.SeatPositionName && l.SeatPositionCode == seatPosition.SeatPositionCode))
            {
                return;
            }
            _context.SeatPositions.Add(seatPosition);
            _context.SaveChanges();
        }

        private void AddPlanModeIfNotExists(PlanMode planMode)
        {
            if (_context.PlanModes.Any(l => l.PlanModeName == planMode.PlanModeName && l.PlanModeCode == planMode.PlanModeCode))
            {
                return;
            }
            _context.PlanModes.Add(planMode);
            _context.SaveChanges();
        }

        private void AddProductModeIfNotExists(ProductMode productMode)
        {
            if (_context.ProductModes.Any(l => l.ProductModeName == productMode.ProductModeName && l.ProductModeCode == productMode.ProductModeCode))
            {
                return;
            }
            _context.ProductModes.Add(productMode);
            _context.SaveChanges();
        }


        //static InitialDatasCreator()
        //{
        //    //#region 工厂


        //    //Factorys = new List<Factory>
        //    //{
        //    //    new Factory("北京工厂","BJ")
        //    //};
        //    //#endregion

        //    //#region 车间

        //    //Workshops = new List<Workshop>
        //    //{
        //    //    new Workshop(1,"北京一期车间","BJCJ001"),
        //    //    new Workshop(1,"北京二期车间","BJCJ002"),
        //    //};

        //    //#endregion

        //    //#region 线体
        //    //Lines = new List<Line>
        //    //{
        //    //    new Line(1,1,"一线","X001"),
        //    //    new Line(1,1,"二线","X002"),
        //    //    new Line(1,1,"三线","X003")
        //    //};



        //    //#endregion

        //    //#region 仓库
        //    //Storages = new List<Storage>
        //    //{
        //    //    new Storage("一号库","K001"),
        //    //    new Storage("二号库","K002")
        //    //};

        //    //#endregion

        //    //#region 颜色
        //    //Colors = new List<Color>
        //    //{
        //    //    new Color("米黄色","Y"),
        //    //    new Color("黑色","B"),
        //    //    new Color("棕色","G"),
        //    //    new Color("酒红色","R"),
        //    //};

        //    //#endregion

        //    //#region 材质
        //    //Materials = new List<Material>
        //    //{
        //    //    new Material("布","C"),
        //    //    new Material("皮","L"),
        //    //    new Material("绒","M"),
        //    //    new Material("革","G"),
        //    //};
        //    //#endregion

        //    //#region 车型
        //    //Cartypes = new List<Cartype>
        //    //{
        //    //    new Cartype("MD车型","MDC"),
        //    //    new Cartype("CF车型","CF"),
        //    //    new Cartype("DM车型","DM"),
        //    //    new Cartype("ID车型","ID"),
        //    //};
        //    //#endregion

        //    //#region 位置
        //    //SeatPositions = new List<SeatPosition>
        //    //{
        //    //    new SeatPosition("左驾","L"),
        //    //    new SeatPosition("右驾","R"),
        //    //    new SeatPosition("坐垫","C"),
        //    //    new SeatPosition("靠背","B"),
        //    //};

        //    //#endregion

        //    //#region 计划模式
        //    //PlanModes = new List<PlanMode>
        //    //{
        //    //    new PlanMode("备品计划","P"),
        //    //    new PlanMode("MES直序列计划","J")
        //    //};

        //    //#endregion

        //    //#region 生产模式
        //    //ProductModes = new List<ProductMode>
        //    //{
        //    //    new ProductMode("备品模式","P"),
        //    //    new ProductMode("MES直序列模式","J")
        //    //};
           
        //    //#endregion

        //}
    }
}